"""
import sys
import os

# 将 my_mysql_lib 目录添加到 sys.path
sys.path.append(os.path.join('/root/06_git', 'my_mysql_lib'))

#yaml文件修改绝对路径
config_path = '/root/06_git/my_mysql_lib/mysql_config.yaml'
"""


from db_config import load_config
from db_operations import query_data, insert_data
from logger_config import logging

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)


def test(config):
    """
    Test database query and insert operations.

    Parameters:
        config (dict): Database configuration dictionary.

    Returns:
        None
    """
    table = '01_tg_msg_table'  # Dynamically specify the table name

    # Query data
    query_condition = "hash_id = %s"
    query_param = '4306561396118471863'
    query_result = query_data(config, table, query_condition, (query_param,))
    if query_result is not None:
        logger.info(f"Query successful, result count: {len(query_result)}")
        for row in query_result:
            logger.info(row)
    else:
        logger.error("Query failed")

    # Insert data
    insert_data_dict = {
        'hash_id': '4306561396118471869',
        'msg_text': 'hello word!',
        'record_json': '{"a": "a"}'
    }
    insert_result = insert_data(config, table, insert_data_dict)
    if insert_result != -1:
        logger.info(f"Insert successful, affected rows: {insert_result}")
    else:
        logger.error("Insert failed")


if __name__ == "__main__":
    # Load database configuration
    config_path = 'mysql_config.yaml'
    config = load_config(config_path)

    # Execute test function
    test(config)
